import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class ClientGUI {

	private JFrame frame;
	private JList<String> list;
	private JButton btnBestelProduct, btnNieuweBestelling, btnBevestig,
			btnOverzicht;
	private JTextField textField;
	private SaxtiehuisClient client;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientGUI window = new ClientGUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ClientGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 332, 287);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new CardLayout(0, 0));

		final JPanel panel = new JPanel();
		frame.getContentPane().add(panel, "login");
		panel.setLayout(null);

		JLabel lblWelkomVulUw = new JLabel("Welkom, vul uw klantnaam in:");
		lblWelkomVulUw.setBounds(91, 42, 190, 26);
		lblWelkomVulUw.setVerticalAlignment(SwingConstants.TOP);
		panel.add(lblWelkomVulUw);

		textField = new JTextField();
		textField.setBounds(91, 65, 143, 20);
		panel.add(textField);
		textField.setColumns(10);

		JButton btnNewButton = new JButton("Naar de winkel");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (textField.getText().equalsIgnoreCase("")) {
					JOptionPane.showMessageDialog(null, "Vul een naam in!",
							"Fout", JOptionPane.OK_OPTION);
				} else {
					CardLayout layout = (CardLayout) frame.getContentPane()
							.getLayout();
					layout.show(frame.getContentPane(), "winkel");
					client = new SaxtiehuisClient(textField.getText());
					vulLijst();
				}
			}
		});
		btnNewButton.setBounds(101, 96, 122, 23);
		panel.add(btnNewButton);

		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, "winkel");
		panel_1.setLayout(null);

		list = new JList<String>();
		list.setBounds(10, 69, 296, 169);
		panel_1.add(list);

		btnNieuweBestelling = new JButton("Nieuwe bestelling");
		btnNieuweBestelling.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(client.maakBestellingAan()){
					JOptionPane.showMessageDialog(null, "Bestelling aangemaakt");
					toggleButtons();
				}else{
					JOptionPane.showMessageDialog(null, "Bestelling niet aangemaakt", "Fout", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNieuweBestelling.setBounds(10, 11, 138, 23);
		panel_1.add(btnNieuweBestelling);

		btnBevestig = new JButton("Bevestigen");
		btnBevestig.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(client.bevestigBestelling()){
					JOptionPane.showMessageDialog(null, "Bestelling geplaatst");
					toggleButtons();
				}else{
					JOptionPane.showMessageDialog(null, "Bestelling niet geplaatst", "Fout", JOptionPane.ERROR_MESSAGE);
				}
			}
			
		});
		btnBevestig.setBounds(10, 35, 138, 23);
		btnBevestig.setEnabled(false);
		panel_1.add(btnBevestig);

		btnOverzicht = new JButton("Besteloverzicht");
		btnOverzicht.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String overzicht = client.getOverzicht();
				JOptionPane.showMessageDialog(null, overzicht);
			}
		});
		btnOverzicht.setBounds(174, 35, 132, 23);
		btnOverzicht.setEnabled(false);
		panel_1.add(btnOverzicht);

		btnBestelProduct = new JButton("Bestel product");
		btnBestelProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String aantalStr = JOptionPane
						.showInputDialog("Vul het aantal in");
				if (aantalStr != null) {
					try{
						int aantal = Integer.parseInt(aantalStr);
						if(client.bestelProduct(list.getSelectedValue(), aantal)){
							JOptionPane.showMessageDialog(null, "Product toegevoegd aan bestelling");
						}else{
							JOptionPane.showMessageDialog(null, "Product niet toegevoegd aan bestelling", "Fout", JOptionPane.ERROR_MESSAGE);

						}
					}catch(NumberFormatException e){
						JOptionPane.showMessageDialog(null,"Geen geldig aantal!");
					}
				}
			}
		});
		btnBestelProduct.setBounds(174, 11, 132, 23);
		btnBestelProduct.setEnabled(false);
		panel_1.add(btnBestelProduct);
	}

	public void vulLijst() {
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		for (String product : client.getProducten()) {
			listModel.addElement(product);
		}
		list.setModel(listModel);
		list.setSelectedIndex(0);
	}

	public void toggleButtons() {
		btnBestelProduct.setEnabled(!btnBestelProduct.isEnabled());
		btnBevestig.setEnabled(!btnBevestig.isEnabled());
		btnOverzicht.setEnabled(!btnOverzicht.isEnabled());
	}
}
